﻿<?php
/**  
* SupPrtDAO.php
*   
*/

require_once(CLASS_PATH."/__autoload.php");

class SupPrtDAO extends Searchmgr
{
	protected $arrStr = array(), $q;
		
	protected function getQuery($keywords)
	{
		if(preg_match('/^s\d{5}/i', $keywords[0])) 
		{
			$table 		 = '  list_link AS ll, list_printer AS lp ';	
			$whereClause =  " (ll.sSerial LIKE '%$keywords[0]%')  AND (lp.pSerial = ll.pSerial) "; 
			$fields = 'lp.pSerial, lp.pType, lp.pMaker, lp.pName, lp.pComment ';
			$order  = 'lp.pMaker, lp.pName ';				  
			$qry 	= $this->db_select($table, $whereClause, $fields, $order);
			
			
		    $q = "SELECT pSerial FROM list_link WHERE (sSerial = '$keywords[0]')";			
			$q = $this->db_query($q);
			
			while($row = mysqli_fetch_array($q)) 
			{
			  $arrStr[] = $row['pSerial'];				
			}
			
			if(empty($arrStr)) return false;
			
			$q = implode(', ', $arrStr);
			$this->recSearchLog($q, 'p');
			
			return $qry;  
		}
		else
		{
/*			
			$keywords = implode('", "', $keywords);	
			$fields = 'pSerial, pType, pMaker, pName, pComment ';
			$order  = 'pMaker, pName ';	
			$qry 	= "SELECT $fields FROM list_printer  WHERE pSerial IN (\"$keywords\") ORDER BY $order";	
/**/

			$log_keywords = implode('", "', $keywords);	
			$this->recSearchLog($log_keywords, 'p');

			$keywords = implode('") OR (hp.pSerial LIKE  "', $keywords);	
			$keywords = '((hp.pSerial LIKE  "'.$keywords.'"))';
			
			$table  = ' list_printer AS lp, haystack_printer AS hp ';
			$fields = 'lp.pSerial, lp.pType, lp.pMaker, lp.pName, lp.pComment, hp.psSerial ';
			$order  = 'lp.pMaker, lp.pType DESC, lp.pName ';
			
			$qry    = "SELECT $fields FROM $table  WHERE $keywords  AND (lp.pSerial = hp.pSerial) ORDER BY $order";		
			return $this->db_query($qry);  	
		}
	}
	
}
?>
